package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.adapter.MyVoucherListAdapter;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.MyVoucherEntity;
import com.sondon.mayi.events.CheckboxEvent;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;
/**
 * 
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : MyVoucher
 * @Author : 蔡文锋
 * @DateTime：2015年6月28日 下午11:38:29
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { 我的代金劵 }
 *
 */
@EActivity(R.layout.myvoucher)
public class MyVoucherActivity extends BaseActivity {

	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	ListView myvoucher_list;
	@Bean
	MyVoucherListAdapter adapter;

	//结果码  
	public final  static int resultCode=0X11;//没有选择
	public final  static int resultCode2=0X22;//有选择
	
	@AfterViews
	public void initView() {

		topBar.setTopBarClickListener(new TopBarClickListener() {

			@Override
			public void rightBtnClick() {

			}

			@Override
			public void leftBtnClick() {
					if(myprefs.is_forResult().get()){
						//设置forResult状态为false
						myprefs.is_forResult().put(false);
						
						//获取启动该activity之前的activity对应的Intent
						Intent mIntent=getIntent();
						// 设置结果，并进行传送  
						MyVoucherActivity.this.setResult(resultCode, mIntent);
						MyVoucherActivity.this.finish();
					}else{
						onBackPressed();
					}
				
			}
		});
		
		//设置适配器
		myvoucher_list.setAdapter(adapter);
		//获取我的优惠券列表
		AsyncHttpUtil.get(URLConfigs.MyVoucher_ukey+myprefs.Ukey().get(), null, new MyVoucherListener());
	}
	
	@Override
	public void onBackPressed() {
		if(myprefs.is_forResult().get()){
			//设置forResult状态为false
			myprefs.is_forResult().put(false);
			
			//获取启动该activity之前的activity对应的Intent
			Intent mIntent=getIntent();
			// 设置结果，并进行传送  
			MyVoucherActivity.this.setResult(resultCode, mIntent);
			MyVoucherActivity.this.finish();
		}else{
			super.onBackPressed();
		}
		
	}
	
	//Listview里的item点击事件
	@ItemClick(R.id.myvoucher_list)
	public void itemOnClick(int position){
		
	}
	
	//请求代金劵列表数据
	class MyVoucherListener extends TextHttpResponseHandler{

		@Override
		public void onSuccess(int status, Header[] arg1, String json) {
			if (status==200) {
				LogUtil.e(TAG, "myvalucher  json : "+json);
				MyVoucherEntity entity=GsonUtil.GetFromJson(json, MyVoucherEntity.class);
				if(entity.isStatus()){
					adapter.SetDatas(entity.getList());
				}
					
			}
		}
		
		@Override
		public void onFailure(int status, Header[] arg1, String json,
				Throwable arg3) {
			ToastUtils.show(context,R.string.network_unavailable);
		}
	}
	
	//代金劵被选择eventbus事件
 	public void onEventMainThread(CheckboxEvent event) {
 		super.onEventMainThread(event);
 		if(myprefs.is_forResult().get()){
 			int position=event.getPosition();
			//设置forResult状态为false
			myprefs.is_forResult().put(false);
			
			//获取启动该activity之前的activity对应的Intent
			Intent mIntent=getIntent();
			
			Bundle data=new Bundle();
			data.putString("voucher_id", adapter.GetDatas().get(position).getId());
			data.putString("value", adapter.GetDatas().get(position).getValue());
			mIntent.putExtras(data);
			
			// 设置结果，并进行传送  
			MyVoucherActivity.this.setResult(resultCode2, mIntent);
			MyVoucherActivity.this.finish();
		}
 		}
}
